# coding=utf-8
# author:  Pleiadi

from messenger.proto.bw import find_criteria
from messenger.gui.Scaleform.BattleEntry import BattleEntry
from ChatManager import ChatManager
from messenger.m_constants import BATTLE_CHANNEL


# noinspection PyPep8Naming
class g_instance(object):
    def __init__(self):
        pass


# noinspection PyPep8Naming
class channels(object):
    def __init__(self):
        pass

    squadChannelId = None
    teamChannelId = None
    commonChannelId = None


# noinspection PyPep8Naming
@staticmethod
def onChannelsListReceived(self, squad, team, common):
    return


# noinspection PyPep8Naming
@staticmethod
def getSquadChannel():
    g_instance.channels.checkStatus()
    return type('obj', (object,), {'cid': g_instance.channels.squadChannelId})


# noinspection PyPep8Naming
@staticmethod
def getTeamChannel():
    g_instance.channels.checkStatus()
    return type('obj', (object,), {'cid': g_instance.channels.teamChannelId})


# noinspection PyPep8Naming
@staticmethod
def getCommonChannel():
    g_instance.channels.checkStatus()
    return type('obj', (object,), {'cid': g_instance.channels.commonChannelId})


# noinspection PyPep8Naming
@staticmethod
def hasSquadChannel():
    g_instance.channels.checkStatus()
    return g_instance.channels.squadChannelId is not None


# noinspection PyPep8Naming
@staticmethod
def hasTeamChannel():
    g_instance.channels.checkStatus()
    return g_instance.channels.teamChannelId is not None


# noinspection PyPep8Naming
@staticmethod
def hasCommonChannel():
    g_instance.channels.checkStatus()
    return g_instance.channels.commonChannelId is not None


# noinspection PyPep8Naming
@staticmethod
def checkStatus():
    if g_instance.channels.teamChannelId is None:
        g_instance.channels.setChannels()


# noinspection PyPep8Naming
@staticmethod
def setChannels():
    m_channels = BattleEntry().channelsStorage.getChannelsByCriteria(find_criteria.BWAllChannelFindCriteria())
    for c in m_channels:
        if 'channels/squad' in c.getFullName() and c.isJoined():
            # noinspection PyPep8Naming
            g_instance.channels.squadChannelId = c.getID()
        elif 'battle/team' in c.getFullName() and c.isJoined():
            # noinspection PyPep8Naming
            g_instance.channels.teamChannelId = c.getID()
        elif 'battle/common' in c.getFullName() and c.isJoined():
            g_instance.channels.commonChannelId = c.getID()


# noinspection PyPep8Naming
def new_setJoined(self):
    old_setJoined(self)
    if self._receiver == BATTLE_CHANNEL.SQUAD[1]:
        g_instance.channels.squadChannelId = self._channelID
    elif self._receiver == BATTLE_CHANNEL.TEAM[1]:
        g_instance.channels.teamChannelId = self._channelID
    elif self._receiver == BATTLE_CHANNEL.COMMON[1]:
        g_instance.channels.commonChannelId = self._channelID
    g_instance.channels.onChannelsListReceived(g_instance.channels(), g_instance.channels.squadChannelId,
                                               g_instance.channels.teamChannelId, g_instance.channels.commonChannelId)


from messenger.gui.Scaleform.view.BattleChannelView import BattleChannelView

old_setJoined = BattleChannelView.setJoined
BattleChannelView.setJoined = new_setJoined


#!tests!

#g_instance.channels.setSquadChannelId('ok')
#print g_instance.channels.getSquadChannel().cid
#if g_instance.channels.hasSquadChannel():
#	print 'ok'

#if g_instance:
#	print 'ok'
